package com.example.demo2.service.impl;

import com.example.demo2.mapper.AdminMapper;
import com.example.demo2.pojo.PageBean;
import com.example.demo2.pojo.data.SupplierDO;
import com.example.demo2.utils.DataUtils;
import com.example.demo2.utils.Result;
import com.example.demo2.pojo.data.AdminDO;
import com.example.demo2.pojo.vo.AdminVO;
import com.example.demo2.service.AdminService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/** 
 * <br/>
 * Created by weidong on 2020/02/02
 */
@Service("adminService")
public class AdminServiceImpl implements AdminService {

	@Resource
	private AdminMapper adminMapper;

	@Override 	public Result add(AdminVO adminVO) throws Exception {
		Integer count = null;
		try {
			count = adminMapper.add(adminVO);
		} catch (Exception e) {
			return Result.getFailure("添加失败");
		}
		if (count > 0) {
			return Result.getSuccess("添加成功");
		}else{
			return Result.getSuccess("添加失败");
		}
	}

	@Override
	public Result updateById(AdminVO adminVO) throws Exception {
		Integer count = adminMapper.updateById(adminVO);
		if (count > 0) {
			return Result.getSuccess("操作成功");
		}else{
			return Result.getSuccess("操作失败");
		}
	}
	
	@Override
	public Result findById(Integer id) throws Exception{
		AdminDO adminDO = adminMapper.findById(id);
		if (adminDO != null) {
			Result result = new Result();
			result.setMsg("操作成功");
			result.setData(adminDO);
			result.setCode(Result.CODE_SUCCESS);
			return result;
		}else{
			return Result.getFailure("操作失败");
		}
	}

	@Override
	public Result list(AdminVO adminVO) throws Exception{
		List<AdminDO> adminDOs = adminMapper.list(adminVO);
		if (adminDOs != null) {
			Result result = new Result();
			result.setMsg("查询成功");
			result.setData(adminDOs);
			result.setCode(Result.CODE_SUCCESS);
			return result;
		}else{
			return Result.getFailure("操作失败");
		}
	}
	int pageSize=10;
	int navigatePages=3;
	@Override
	public PageBean listPage(AdminVO adminVO) throws Exception{
		//设置分页数据大小
		adminVO.setPageSize(pageSize);
		//查询数目的总个数
		Integer count=adminMapper.count(adminVO);
		if(count==0){
			PageBean pageBean=new PageBean(null,pageSize,adminVO.getCurrentPage(),count,navigatePages);
			//数据库没有数据
			return pageBean;
		}

		//判断页码是否越界,如果页码大于最大值,返回最后一页数据
		//计算最大页码数
		int max= DataUtils.getPageCount(count,pageSize);
		if(adminVO.getCurrentPage()>max){
			adminVO.setCurrentPage(max);
		}

		//设置查询起始位置
		adminVO.setStartRecord(DataUtils.getStartRecord(adminVO.getCurrentPage(),pageSize));

		//调用mapper查询数据
		List<AdminDO> flatsDOS=adminMapper.listPage(adminVO);
		PageBean pageBean=new PageBean(flatsDOS,pageSize,adminVO.getCurrentPage(),count,navigatePages);

		return pageBean;
	}
	
	@Override
	public Result count(AdminVO adminVO) throws Exception{
		return null;
	}
	
	@Override
	public Result deleteById(Integer id) throws Exception{
		Integer count = adminMapper.deleteById(id);
		if (count > 0) {
			return Result.getSuccess("添加成功");
		}else{
			return Result.getFailure("操作失败");
		}
	}

	@Override
	public AdminDO login(String username, String password) {
		AdminVO adminVO = new AdminVO();
		adminVO.setUsername(username);
		List<AdminDO> adminDOS = adminMapper.list(adminVO);
		if (adminDOS != null && !adminDOS.isEmpty()) {
			AdminDO adminDO = adminDOS.get(0);
			if (adminDO.getPassword().equals(password)) {
				return adminDO;
			}
		}
		return null;
	}

	@Override
	public AdminDO findByName(String username) {
		return adminMapper.queryUserByName(username);
	}

}